# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=poppler
pkgbase=mingw-w64-${_realname}-qt6
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-qt6"
pkgver=21.09.0
pkgrel=1
pkgdesc="PDF rendering library based on xpdf 3.0 (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://poppler.freedesktop.org/"
license=("GPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-curl"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-lcms2"
             "${MINGW_PACKAGE_PREFIX}-libjpeg"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-nss"
             "${MINGW_PACKAGE_PREFIX}-openjpeg2"
             "${MINGW_PACKAGE_PREFIX}-poppler-data"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-poppler=${pkgver}"
         "${MINGW_PACKAGE_PREFIX}-qt6-base")
options=('strip' 'staticlibs')
source=(https://poppler.freedesktop.org/${_realname}-${pkgver}.tar.xz{,.sig}
        test::git+https://gitlab.freedesktop.org/poppler/test.git#commit=03a4b9eb854a06a83c465e82de601796c458bbe9)
sha256sums=('5a47fef738c2b99471f9b459a8bf8b40aefb7eed92caa4861c3798b2e126d05b'
            'SKIP'
            'SKIP')
validpgpkeys=('CA262C6C83DE4D2FB28A332A3A6A4DB839EAA6D7') # "Albert Astals Cid <aacid@kde.org>"

prepare() {
  cd ${_realname}-${pkgver}

}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  CXXFLAGS+=" -Wno-deprecated-declarations -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}/bin/cmake.exe" \
    -Wno-dev \
    -G"Ninja" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
    -DSPLASH_CMYK=ON \
    -DENABLE_GLIB=ON \
    -DENABLE_GOBJECT_INTROSPECTION=ON \
    -DENABLE_CPP=ON \
    -DENABLE_DCTDECODER=libjpeg \
    -DENABLE_LIBOPENJPEG=openjpeg2 \
    -DENABLE_CMS=lcms2 \
    -DENABLE_NSS3=ON \
    -DENABLE_UTILS=ON \
    -DENABLE_LIBCURL=ON \
    -DENABLE_ZLIB=ON \
    -DENABLE_ZLIB_UNCOMPRESS=OFF \
    -DENABLE_GTK_DOC=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}
  cp -v libpoppler*.dll qt6/tests
  cp -v cpp/libpoppler-cpp*.dll qt6/tests
  cp -v qt6/src/libpoppler-qt6*.dll qt6/tests

  ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target test || ture
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target qt6/install
  install -Dm644 poppler-qt6.pc "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/poppler-qt6.pc"
}
